package com.mmy.fta.usercenter.rest;

import com.mmy.fta.usercenter.request.DriverMemberShipRequest;
import com.mmy.fta.usercenter.request.ImmediateRenewRequest;
import com.mmy.fta.usercenter.request.MembershipEndTimeRequest;
import com.mmy.fta.usercenter.request.ShipperMemberShipRequest;
import com.mmy.fta.usercenter.response.ImmediateRenewResponse;
import com.mmy.fta.usercenter.service.impl.MembershipServiceImpl;
import com.ymm.common.rpc.YmmResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @author guohao.xie
 * @apiNote
 * @exportable
 * @since 2021/11/2
 */
@RestController
@Slf4j
@RequestMapping(value = "/membership")
public class MemberShipController {

    @Autowired
    MembershipServiceImpl membershipService;

    /**
     * ”我的“页面，显示尊享会员到期时间、剩余发货次数、剩余优惠券次数
     */
    @PostMapping(value = "membershipEndTime")
    public YmmResult membershipEndTime(@RequestBody MembershipEndTimeRequest membershipEndTime) {
        if (Objects.isNull(membershipEndTime)  &&  membershipEndTime.getOpenId() == null) {
            return YmmResult.failResult(0, "请求参数为空");
        }
        try {
            YmmResult result = membershipService.membershipEndTime(membershipEndTime.getOpenId());
            return result;
        } catch (Exception e) {
            log.info(e.toString());
            return YmmResult.failResult(0, "调用失败");
        }
    }
    /**
     * 司机开通或续费会员
     */
    @PostMapping(value = "driverRenew")
    public YmmResult<ImmediateRenewResponse> driverRenew(@RequestBody DriverMemberShipRequest driverMemberShipRequest) {
        if (Objects.isNull(driverMemberShipRequest)) {
            return YmmResult.failResult(0, "请求参数为空");
        }
        try {
            YmmResult result = membershipService.driverRenew(driverMemberShipRequest);
            return result;
        } catch (Exception e) {
            log.info(e.toString());
            return YmmResult.failResult(0, "调用失败");
        }
    }

    /**
     * 货主开通或续费会员
     */
    @PostMapping(value = "shipperRenew")
    public YmmResult<ImmediateRenewResponse> shipperRenew(@RequestBody ShipperMemberShipRequest shipperMemberShipRequest) {
        if (Objects.isNull(shipperMemberShipRequest)) {
            return YmmResult.failResult(0, "请求参数为空");
        }
        try {
            YmmResult result = membershipService.shipperRenew(shipperMemberShipRequest);
            return result;
        } catch (Exception e) {
            log.info(e.toString());
            return YmmResult.failResult(0, "调用失败");
        }
    }

    /**
     * 司机会员“立即续费”展示
     */
    @PostMapping(value = "driverImmediateRenewShow")
    public YmmResult<ImmediateRenewResponse> driverImmediateRenew(@RequestBody ImmediateRenewRequest immediateRenewRequest) {
        if (Objects.isNull(immediateRenewRequest)) {
            return YmmResult.failResult(0, "请求参数为空");
        }
        try {
            YmmResult result = membershipService.driverImmediateRenew(immediateRenewRequest.getToken());
            return result;
        } catch (Exception e) {
            log.info(e.toString());
            return YmmResult.failResult(0, "调用失败");
        }
    }

    /**
     * 货主会员“立即续费”展示
     */
    @PostMapping(value = "shipperImmediateRenewShow")
    public YmmResult<ImmediateRenewResponse> shipperImmediateRenew(@RequestBody ImmediateRenewRequest immediateRenewRequest) {
        if (Objects.isNull(immediateRenewRequest)) {
            return YmmResult.failResult(0, "请求参数为空");
        }
        try {
            YmmResult result = membershipService.shipperImmediateRenew(immediateRenewRequest.getToken());
            return result;
        } catch (Exception e) {
            log.info(e.toString());
            return YmmResult.failResult(0, "调用失败");
        }
    }

}

